﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Cwt;

namespace WebApp.Management._Article
{
    public partial class List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ICwtService service = Global.Get<ICwtService>();
                List<Category> categories = service.FindCategories();
                this._ctlListCategory.DataSource = categories;
                this._ctlListCategory.DataBind();

                if (categories != null && categories.Count > 0)
                {
                    LoadArticleListSource(categories[0].Id);
                }
            }
        }

        protected void _ctlListCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            Guid categoryId = new Guid(this._ctlListCategory.SelectedValue);
            LoadArticleListSource(categoryId);
        }

        private void LoadArticleListSource(Guid categoryId)
        {
            ICwtService service = Global.Get<ICwtService>();
            this._ctlRepeater.DataSource = service.FindArticles(categoryId, 0, 999);
            this._ctlRepeater.DataBind();
        }
    }
}